# 0811pysimplegui按钮打开txt.py
from pathlib import Path
import PySimpleGUI as sg

from _txt2docx import txt2docx
# from _add_hyperlink import add_hyperlink要3个参数
from _trans_hyperlink import trans_link#只要1个参数

'''
def popup_text(filename, text):

    layout = [
        [sg.Multiline(text, size=(80, 25)),],
    ]
    win = sg.Window(filename, layout, modal=True, finalize=True)

    while True:
        event, values = win.read()
        if event == sg.WINDOW_CLOSED:
            break
    win.close()
'''
def opentxt():
    sg.theme("DarkBlue3")
    sg.set_options(font=("Microsoft JhengHei", 12))

    layout = [
        [
            sg.Input(key='-INPUT-'),
            sg.FileBrowse(file_types=(("TXT Files", "*.txt"), ("ALL Files", "*.*"))),#打开浏览
            sg.Button("txt2docx"),
        ]
    ]

    window = sg.Window('Title', layout)

    while True:
        event, values = window.read()
        if event == sg.WINDOW_CLOSED:
            break
        elif event == 'txt2docx':
            filename = values['-INPUT-']
            if Path(filename).is_file():
                try:
                    with open(filename, "rt", encoding='utf-8') as f:#自动根据浏览》选择得到文件路径
                        text = f.read()
                    print(filename)#成功
                    txt2docx(filename)# 【filename是txt】】】
                    filename2=filename.replace(".txt",".docx")
#                     add_hyperlink(filename)3个参数
#                     time.sleep(1)
                    trans_link(filename2)
                    
    #                 popup_text(filename, text)#自定义的函数【】【】【】
#                     layout = [
#                         [sg.Multiline(text, size=(80, 25)),],
#                     ]
#                     win = sg.Window(filename, layout, modal=True, finalize=True)
# 
#                     while True:
#                         event, values = win.read()
#                         if event == sg.WINDOW_CLOSED:
#                             break
#                     win.close()

                except Exception as e:
                    print("Error: ", e)

    window.close()
# opentxt()#无需参数